<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/7
 * Time: 1:51
 */
namespace app\api\controller\v2;
use app\common\model\ArticleCommentReply;
use app\common\model\MessageCenter;
use app\common\model\Recode;
use app\common\model\UserMsg;
use think\Db;

/**个人中心相关接口
 * @title
 * @desc
 * @url
 * @version 2.0.0
 * Class Demo
 * @package app\api\controller\v2
 */
class Personal extends Auth
{
    protected $restActionList = ['index','read','save','update','delete'];

    /**
     * @title
     * @method GET
     * @desc  获取我的信息
     * @return array
     */
    public function index()
    {
        if(!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403 );
        }
        
        $RecodeModel = new Recode();
        $MsgModel = new MessageCenter();
       $uid = $this->user['id'];
        

        //        将用户头像和档案头像统一
        $map =['uid'=>$this->user['id'],'isself'=>1];
        $rea = $RecodeModel->getSelfRecode($map);
        $ra =$rea['avatar'];
        $u =$this->user['avatar'];

        if ($ra)
        {
            if(!$this->user['avatar']|| $u=="")
            {
                Db::table('app_user')->where('id',$this->user['id'])->update(['avatar'=>$rea['avatar']]);
            }
        }
        if ($u)
        {
            if(!$ra || $ra=="")
            {
                Db::table('app_recode')->where($map)->update(['avatar'=>$this->user['avatar']]);
            }
        }
        if ($ra && $u)
        {
            if (strcmp($rea['avatar'],$this->user['avatar']) !== 0)
            {
                Db::table('app_recode')->where($map)->update(['avatar'=>$this->user['avatar']]);
            }
        }

        $recode = $RecodeModel->getRecodeWithAstro(['id' => $rea['id']]);

        if (!$this->user['sex'])
        {
            $sex=1;
        }
        else
            {
            $sex=$this->user['sex'];
        }
        $data = [
            'id'=>$recode['id'],
            'name'=>$recode['name'],
            'sex'=>$sex,
            'avatar'=>$recode['avatar'],
            'sun'=>$recode['sun'],
            'moon'=>$recode['moon'],
            'asc'=>$recode['asc'],
        ];
        //获取用户全部未读系统消息id
        $system_msg_id = $MsgModel->getAllUserMessage(['uid'=>$uid,'type'=>1]);
        //获取用户全部未读服务消息id
        $server_msg_id = $MsgModel->getAllUserMessage(['uid'=>$uid,'type'=>2]);
        $msg_count = count($system_msg_id) +count($server_msg_id);

         // 获取回复的阅读情况
        $replyModel = new ArticleCommentReply();
        $unread_ids =$replyModel->getUnReadReply(['ucid'=>$this->user['uc_id'],'uid'=>$this->user['id']]);
        if ($unread_ids)
        {
            $reply_count=count($unread_ids);
        }
        else
        {
            $reply_count=0;
        }
        $array1['msg_count'] =$msg_count + $reply_count;
        $array=array_merge($data,$array1);
        return show(200,'成功',$array);
    }
}